package com.videoaggre.zcl.common

import com.google.gson.Gson
import com.videoaggre.zcl.bean.QimanHome
import com.videoaggre.zcl.bean.UserInfoBean
import com.videoaggre.zcl.utils.MoshiUtils
import com.videoaggre.zcl.utils.SPUtils

object UserDataCache {
//    private val gson = Gson()
//    var qimanHome: QimanHome? = null
//        set(value) {
//            if (value != null)
//                SPUtils.put(SP_KEY_QIMAN_HOME, gson.toJson(value))
//            field = value
//        }
//        get() = gson.fromJson(SPUtils.getString(SP_KEY_QIMAN_HOME, "{}"), QimanHome::class.java)

    // 用户token
    private var mToken: String? = null

    // 用户基本信息
    private var mUserInfoBean: UserInfoBean? = null
    fun getToken(): String {
        if (mToken == null) {
            mToken = SPUtils.getString(SP_KEY_TOKEN, "")
        }
        return mToken!!
    }

    fun getUserInfoBean(): UserInfoBean {
        if (mUserInfoBean == null) {
            mUserInfoBean = MoshiUtils.parseObject(
                SPUtils.getString(SP_KEY_USER_INFO, "{}"),
                UserInfoBean::class.java
            )
        }
        return mUserInfoBean!!
    }

    fun changeUserInfoBean() {
        saveUserInfoBean(mUserInfoBean)
    }

    fun saveToken(token: String) {
        mToken = token
        SPUtils.put(SP_KEY_TOKEN, token)
    }

    fun saveUserInfoBean(userInfoBean: UserInfoBean?) {
        mUserInfoBean = userInfoBean
        SPUtils.put(SP_KEY_USER_INFO, MoshiUtils.toJSONString(userInfoBean ?: UserInfoBean()))
    }

    /*fun logout(listener: OnStartActivityListener) {
        mToken = null
        mUserInfoBean = null
        SPUtils.remove(SP_KEY_TOKEN)
        SPUtils.remove(SP_KEY_USER_INFO)
        listener.goActicity(LoginActivity::class.java, bundle = Bundle().apply {
            putString(LoginActivity.KEY_SOURCE, LoginActivity.SOURCE_API_NOT_LOGIN)
        })
    }*/
}